Imports System.Windows.Forms

Public Class CSI

    Dim CurrentAnatomySlice As Bitmap
    Dim ScannedAnatomySLice As Bitmap

    Dim CSIAnatomyVolume, CSIDataVolume As MatrixDataStructures.SingleVolume

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        If FormMain.AnatomyVolume Is Nothing Then
            MsgBox("Operation Cannot be completed without Anatomy Data", MsgBoxStyle.OkOnly, "Error")
            Exit Sub
        End If

        CurrentAnatomySlice = FormMain.AnatomyDisplay.CurrentImage(False)

        PictureBox1.Image = CurrentAnatomySlice

        Dim ofd As New OpenFileDialog       'open file dialog
        ofd.FileName = ""
        ofd.Filter = "Philips Header file (*.par)|*.par|Philips Image file (*.rec)|*.rec|All files (*.*)|*.*"
        ofd.Title = "Choose the Anatomy Dataset PAR file"

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            CSIAnatomyVolume = New MatrixDataStructures.SingleVolume
            Dim Loader As New SingleOptimizedParRecLoader(ofd.FileName)
            CSIAnatomyVolume = Loader.GetData
            CSIAnatomyVolume.HeaderFileName = ofd.FileName
        Else
            Exit Sub
        End If
        
        Dim temp(,) As Short = CSIAnatomyVolume.Matrix.TransverseSlice(0)

        Dim colorvalue As Integer, max As Integer, min As Integer
        max = CSIAnatomyVolume.Matrix.Maximum
        min = CSIAnatomyVolume.Matrix.Minimum

        ScannedAnatomySLice = New Bitmap(temp.GetUpperBound(1), temp.GetUpperBound(0))

        For i As Integer = 0 To ScannedAnatomySLice.Height - 1 'height is y
            For j As Integer = 0 To ScannedAnatomySLice.Width - 1 ' width is x
                colorvalue = CInt(((temp(i, j) - min) * 255 / (max - min)))
                ScannedAnatomySLice.SetPixel(i, j, Color.FromArgb(colorvalue, colorvalue, colorvalue))
            Next
        Next

        PictureBox2.Image = ScannedAnatomySLice

        ofd.Title = "Choose the CSI Dataset PAR file"

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            CSIDataVolume = New MatrixDataStructures.SingleVolume
            Dim Loader As New SingleOptimizedParRecLoader(ofd.FileName)
            CSIDataVolume = Loader.GetData
            CSIDataVolume.HeaderFileName = ofd.FileName
        Else
            Exit Sub
        End If

        For i As Integer = 0 To CSIDataVolume.Matrix.z - 1
            ListBox1.Items.Add("Dataset:" & i)
        Next
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FormMain.AnatomyDisplay.ChangeCurrentSliceInfo("t", CInt(IIf(FormMain.AnatomyDisplay.CurrentSliceIndex = 0, FormMain.AnatomyDisplay.CurrentSliceIndex, FormMain.AnatomyDisplay.CurrentSliceIndex - 1)))
        Me.CurrentAnatomySlice = FormMain.AnatomyDisplay.CurrentImage(False)
        PictureBox1.Image = CurrentAnatomySlice
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FormMain.AnatomyDisplay.ChangeCurrentSliceInfo("t", CInt(IIf(FormMain.AnatomyVolume.Matrix.z + 1 = FormMain.AnatomyDisplay.CurrentSliceIndex, FormMain.AnatomyDisplay.CurrentSliceIndex, FormMain.AnatomyDisplay.CurrentSliceIndex + 1)))
        Me.CurrentAnatomySlice = FormMain.AnatomyDisplay.CurrentImage(False)
        PictureBox1.Image = CurrentAnatomySlice
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim temp(,) As Short = CSIDataVolume.Matrix.TransverseSlice(ListBox1.SelectedIndex)

        Dim colorvalue As Integer, max As Integer, min As Integer
        max = CSIAnatomyVolume.Matrix.Maximum
        min = CSIAnatomyVolume.Matrix.Minimum

        Dim SelectedDataSlice As Bitmap = New Bitmap(temp.GetUpperBound(1), temp.GetUpperBound(0))

        For i As Integer = 0 To SelectedDataSlice.Height - 1 'height is y
            For j As Integer = 0 To SelectedDataSlice.Width - 1 ' width is x
                colorvalue = CInt(((temp(i, j) - min) * 255 / (max - min)))
                SelectedDataSlice.SetPixel(i, j, Color.FromArgb(colorvalue, colorvalue, colorvalue))
            Next
        Next
        PictureBox3.Image = SelectedDataSlice
    End Sub
End Class
